<!--
*  2021-03-15
*  退料列表
-->
<template>
  <div id="app" class="material-return">
    <!-- 头部导航 -->
    <van-nav-bar
      title="退料列表"
      ref="header"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
      @click-right="onClickRight"
    >
      <template #right>
        <van-icon name="scan" size="20" />
      </template>
    </van-nav-bar>

    <!-- 滚动 -->
    <better-scroll
      class="public-scroll"
      :data="tableData"
      :totalPage="totalPage"
      :pullup="true"
      :pulldown="true"
      @pullup="onPullup"
      @pulldown="onPulldown"
    >
      <van-cell-group v-show="tableData.length !== 0">
        <van-cell
          v-for="(data, index) in tableData"
          :key="index"
          :title="data.title"
          @click="onClickStart(data)"
        >
          <template slot="label">
            <span>创建时间: {{ data.createAt }}</span>
          </template>
          <template slot="default">
            <span>{{
              data.status === 1 ? "待办" : data.status === 2 ? "在办" : "已办"
            }}</span>
          </template>
        </van-cell>
      </van-cell-group>
      <van-empty
        class="custom-image"
        image="https://img01.yzcdn.cn/vant/custom-empty-image.png"
        v-show="tableData.length === 0"
        description="暂无数据"
      />
    </better-scroll>
  </div>
</template>

<script>
import { openWin } from "@/utils/index";
import {
  getTaskToDo,
  getTaskDoing,
  getWeavingPlan,
  getTaskAccept,
} from "@/api/yarn.js";

export default {
  name: "materialReturn",
  data() {
    return {
      title: "",
      currentPage: 1,
      totalPage: 1,
      tableData: [],
      todoTotalPage: 1,
      doingTotalPage: 1,
    };
  },
  mounted() {
    const that = this;
    if (process.env.NODE_ENV === "production") {
      this.$nextTick(() => {
        that.initFn(that);
      });
    } else {
      window.apiready = function () {
        that.initFn(that);
      };
    }
  },
  methods: {
    //初始化
    initFn(that) {
      that.onGetTableData();
      window.api.addEventListener({
        name:'viewdisappear'
      }, function() {
        window.api.closeWin();
      });
    },
    // 返回列表
    onClickLeft() {
      openWin("yarn-material");
    },
    // 扫码
    onClickRight() {
      const that = this;
      const zyHmsScan = api.require('zyHmsScan');
      zyHmsScan.openDefaultView({}, ret => {
        if(ret.status) {
          let content = ret.result;
          if (!that.$validURL(content)) {
            that.$toast.fail("二位码异常");
            return false;
          }
          getWeavingPlan({ resUrl: content }, (respon) => {
            if (respon[0].status === 1) {
              // 先接单
              getTaskAccept(
                  {
                    taskId: respon[0].taskId,
                    stepNo: respon[0].stepNo,
                  },
                  () => {
                    that.$toast({
                      type: "success",
                      message: "接单成功",
                      onClose: () => {
                        openWin("return-detail", {
                          doingResUrl: respon[0].doingResUrl,
                          flowType: respon[0].flowType,
                          stepId: respon[0].id,
                          resId: respon[0].resId,
                          taskId: respon[0].taskId,
                          stepNo: respon[0].stepNo,
                          nextStepNo: respon[0].nextStepNo,
                        });
                      },
                    });
                  }
              );
            } else {
              openWin("return-detail", {
                doingResUrl: respon[0].doingResUrl,
                flowType: respon[0].flowType,
                stepId: respon[0].id,
                resId: respon[0].resId,
                taskId: respon[0].taskId,
                stepNo: respon[0].stepNo,
                nextStepNo: respon[0].nextStepNo,
              });
            }
          });
        }
      });
    },
    // 获取列表
    onGetTableData() {
      this.onGetTaskToDo();
      this.onGetTaskDoing();
    },
    // 任务列表  待办列表
    onGetTaskToDo() {
      this.todoTotalPage = 1;
      let params = {};
      params.depId = 45;
      params.postId = 238;
      params.pageNum = this.currentPage;
      params.pageSize = 1000;
      getTaskToDo(params, (res) => {
        this.tableData = this.tableData.concat(res.list);
        this.todoTotalPage = res.todoTotalPage;
      });
    },
    // 任务列表  在办列表
    onGetTaskDoing() {
      this.doingTotalPage = 1;
      let params = {};
      params.depId = 45;
      params.postId = 59;
      params.pageNum = this.currentPage;
      params.pageSize = 1000;
      getTaskDoing(params, (res) => {
        this.doingTotalPage = res.doingTotalPage;
        this.tableData = this.tableData.concat(res.list);
      });
    },
    // 下拉刷新
    onPulldown() {
      this.currentPage = 1;
      this.totalPage = 1;
      this.tableData = [];
      this.onGetTableData();
    },
    // 上拉加载
    onPullup() {
      this.currentPage++;
      this.onGetTableData();
    },
    // 点击 待办 （先接单在doing） 在办 doing  已办 done
    onClickStart(data) {
      const that = this;
      if (data.status === 1) {
        getTaskAccept(
          {
            taskId: data.taskId,
            stepNo: data.stepNo,
          },
          () => {
            that.$toast({
              type: "success",
              message: "接单成功",
              onClose: () => {
                openWin("return-detail", {
                  doingResUrl: data.doingResUrl,
                  flowType: data.flowType,
                  stepId: data.id,
                  resId: data.resId,
                  taskId: data.taskId,
                  stepNo: data.stepNo,
                  nextStepNo: data.nextStepNo,
                });
              },
            });
          }
        );
      } else {
        openWin("return-detail", {
          doingResUrl: data.doingResUrl,
          flowType: data.flowType,
          stepId: data.id,
          resId: data.resId,
          taskId: data.taskId,
          stepNo: data.stepNo,
          nextStepNo: data.nextStepNo,
        });
      }
    },
  },
};
</script>

<style lang="scss">
.material-return {
  // padding-top: 22px;

  .public-scroll {
    height: calc(100vh - 74px);
    overflow: hidden;
  }
}
</style>
